package c3g;

import c3g.Rex;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.FieldDoc;
import com.sun.javadoc.RootDoc;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import java.io.*;
import java.util.ArrayList;

public class Enum {
    private ArrayList<Rex> list =new ArrayList<>();
    public void ShowEunm(RootDoc rootDoc, Rex Rex){

        ClassDoc mes=null;
        String sbr="";
        ClassDoc[] classes = rootDoc.classes();
        //反射到整个类
        for(ClassDoc classDoc : classes ){
            if(Rex.getType().equals("")){
                Rex.setType(classDoc.toString());
                Rex.setName(Rex.getType().split("\\."));
            }
            ClassDoc[] methodDocs = classDoc.innerClasses();
            //反射到枚举方法
            for(ClassDoc publics :methodDocs){
                ClassDoc me = publics;
                String name =me.simpleTypeName();
                String e =  me.commentText();
                FieldDoc[] tt=publics.fields() ;
                Rex Rex1 =new Rex();
                //反射到属性
                String Ishave="";
                for (FieldDoc text:tt){

                    FieldDoc Text=text;
                    String comment =Text.commentText();
                    if(comment.equals("")){
                        break;
                    }
                    if(me != mes){
                        Rex1.getData().append("enum ").append(name).append("{"+"\r");
                        mes=me;
                    }
                    //存每个枚举的第一个注释
                    if(Ishave.isEmpty()) {
                        Ishave=Text.commentText();

                    }
                    //判断是否重复
                    else if(Ishave.equals(comment)){break;}
                    //判断是否连续重复
                    if(!comment.equals(sbr)){
                        //进行切割获取需要的部分
                        boolean iscode =comment.contains("<code>");
                        String[] code = comment.split("<code>");
                        String[] code1 =code[1].split("</code>");
                        boolean status =comment.contains("<pre>");
                        if(status){
                            String[] pre =comment.split("<pre>");
                            String[] pre1=pre[1].split("</pre>");
                            String comtxt =pre1[0].replace("\n","");
                            Rex1.getData().append(code1[0]).append("//").append(comtxt).append("\r");

                        }
                        else {
                            Rex1.getData().append(code1[0]).append("\r");

                        }
                        sbr =Text.commentText();
                    }
                }
                if(!Rex1.getData().toString().equals("")){
                    this.list.add(Rex1);
                    Rex1.getData().append("\r"+"}");

                }

            }
        }
    }
    public void build(Rex Rex,String enumvm,String path) throws IOException {
        //判断out文件夹是否存在，不存在则创建
        File desfile =new File(path);
        if(!desfile.exists()){
            desfile.mkdir();
        }
        desfile.createNewFile();
        //设置模板、容器
        VelocityEngine ve =new VelocityEngine();
        VelocityContext velocityContext =new VelocityContext();  //生成对象
        ve.init();
        Template template =ve.getTemplate(enumvm);   //设置模板文件
        velocityContext.put("Rex",Rex);  //设置容器
        velocityContext.put("list",this.list);
        StringWriter stringWriter =new StringWriter();
        template.merge(velocityContext,stringWriter);
        String r =stringWriter.toString();
        //生成文件
        Writer wr = new FileWriter(path+"/"+Rex.getName()[1]+".proto");
        wr.append(r);
        wr.flush();
        wr.close();
    }
}
